/* * @Author: chenjie * @Date: 2019-01-21 14:24:19 * @Last Modified by: hhy *
:page-size="propsPageSize" :current-page="currPage" @Last Modified time:
2019-03-26 10:18:46 * @文件说明: 分页 */
<template>
  <div class="h-page">
    <span class="total"> 共 {{ total }} 条 </span>
    <el-pagination
      v-model:currentPage="propsCurrPage"
      :page-size="propsPageSize"
      layout="prev, pager, next"
      :total="total"
      @current-change="handleCurrentChange"
      background
    >
    </el-pagination>
    <div class="jump-box" v-if="total / this.propsPageSize > 7">
      <el-input-number
        v-model="jumpPage"
        :controls="false"
        class="jump-input"
      ></el-input-number>
      <el-button type="primary" size="small" class="jump-btn" @click="pageJump"
        >跳转</el-button
      >
    </div>
  </div>
</template>

<script>
import { defineComponent, toRefs, reactive } from "vue";

export default defineComponent({
  name: "all-page",
  inheritAttrs: false,
  props: {
    currPage: Number,
    total: Number,
    size: Number,
  },
  emits: ["change", "currPage"],
  computed: {
    propsCurrPage() {
      return Number(this.currPage);
    },
    propsPageSize() {
      return Number(this.size || 20);
    },
  },
  setup(props, context) {
    const state = reactive({
      currentPage1: 2,
      jumpPage: 1,
    });
    const handleCurrentChange = (val) => {
      context.emit("update:currPage", val);
      context.emit("change", val);
    };

    const pageJump = () => {
      if (state.jumpPage > 0) {
        state.jumpPage = parseInt(state.jumpPage);
        if (state.jumpPage > props.total / props.size) {
          const num = props.total / props.size;
          state.jumpPage = parseInt(num) + 1;
        }
      } else {
        state.jumpPage = 1;
      }
      context.emit("update:currPage", state.jumpPage);
      console.log(state.jumpPage);
      context.emit("change", state.jumpPage);
    };
    return {
      ...toRefs(state),
      handleCurrentChange,
      pageJump,
    };
  },
});
</script>

<style lang="scss" scoped>
.h-page {
  margin-top: 30px;
  width: 100%;
  text-align: right;
  padding-right: 20px;
  box-sizing: border-box;
  ::v-deep(.el-pagination) {
    @include c9;

    font-weight: normal;
    .btn-next .el-icon,
    .btn-prev .el-icon {
      font-size: 14px;
    }
    .number {
      font-size: 14px;
    }
    * {
      display: inline-block;
    }
  }
  ::v-deep(.el-pagination__total) {
    @include c9;
    font-size: 14px;
  }
}

div {
  display: inline-block;
}
.jump-box {
  display: inline-block;
  .el-input--small .el-input__inner {
    height: 30px;
    line-height: 30px;
  }

  .jump-input {
    width: 60px;
  }
  .jump-btn {
    margin-left: 4px;
    background-color: $lpoll-theme-color !important;
  }
}
</style>
